package com.sfac.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.account.service.UserService;
import com.sfac.javaSpringBoot.modules.test.entity.Result;
import com.sfac.javaSpringBoot.modules.test.entity.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//UserController中所提供的方法，路径是在底层实现的，在Postman中检查是否已实现相应的方法

/**
 * 用户控制类
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1/api/user ---- post
     * {"userName":"CJR","password":"root"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /**
     * 127.0.0.1/api/user ---- put更新id为3的用户
     * {"id":3,"userName":"CJR","password":"root"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    public Result<User> updatetUser(@RequestBody User user) {
        return userService.updatetUser(user);
    }

    /**
     *127.0.0.1/api/user/1  -----get获取id为1的用户
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id){
        return userService.getUserById(id);
    }


    /**
     * 127.0.0.1/api/login  -----post
     * {"userName":"CJR","password":"root"}
     */
    @PostMapping(value = "/login",consumes = "application/json")
    public Result<User> getUserByUserNameAndPassword(@RequestBody User user){
        return userService.getUserByUserNameAndPassword(
                user.getUserName(),user.getPassword());
    }

    /**
     *127.0.0.1/api/user/1  -----delete
     */
    @DeleteMapping("/user/{id}")
    public Result<Object> deleteUserById(@PathVariable int id){
        return userService.deleteUserById(id);
    }

    /**
     * 多条件的分页查询接口，在users.html中实现
     *127.0.0.1/api/users ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"id","direction":"asc","keyword":""}
     */

    @PostMapping(value = "/users",consumes = "application/json")
    public PageInfo<User> getUsersBySearch(@RequestBody Search search){
        return userService.getUsersBySearch(search);
    }



}
